# -*- coding: utf-8 -*-

from crmv2.survey.models import Survey, SurveyTemplate, SurveyQuestion, \
    SurveyNotifySchedule
from django.contrib import admin

from crm.models import Salesperson

class SurveyNotifyScheduleInline(admin.TabularInline):
    model = SurveyNotifySchedule
    extra = 1
    verbose_name = "notify at"
    max_num = 3


class SurveyAdmin(admin.ModelAdmin):
    fields = ('title', 'created_at', 'owner', 'description', 'template')
    list_per_page = 20
    list_display = ['title', 'created_at', 'owner', 'template', 'closed', 'n_invitations', 'n_answers']
    search_fields = ['title', 'owner__name']
    list_filter = ['closed', 'template', 'created_at', 'owner']
    inlines = [SurveyNotifyScheduleInline]
    actions = ['close_survey']
    readonly_fields = ['created_at']

    def close_survey(self, request, queryset):
        queryset.update(closed = True)

class QuestionInline(admin.TabularInline):
    model = SurveyQuestion

class SurveyTemplateAdmin(admin.ModelAdmin):
    fieldsets = [
        (None, {'fields' : ['name', 'description']}),
    ]
    inlines = [QuestionInline]

class SurveyInvitationAdmin(admin.ModelAdmin):
    list_display = ['name', 'email', 'answered']
    list_filter = ['answered']

admin.site.register(Survey, SurveyAdmin)
admin.site.register(SurveyTemplate, SurveyTemplateAdmin)
admin.site.register(Salesperson)
